<?php

require_once './application/core/CoreBase.php';
require_once './application/models/Objets/Composant.php';
require_once './application/entity/config/conflist.php';
require_once './application/entity/config/fileconf.php';

class listConfig extends CoreBase
{

    private $_list;
    private $_identity;
    private $_param;
    private $_ip;
    private $_obj;
    private $_objCore;
    private $_id;

    public function getListConfig()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_objCore = new CoreBase($this->_controller);
        $this->_identity = $this->_objCore->listIdentityFilter();
        $this->_param = $this->_objCore->listParamFilter();
        $this->_list = false;
        if (!isset($this->_ip) || strlen($this->_ip) == 0)
        {
            $this->objectListConf();
            return false;
        }
        $this->_ip = $this->_objCore->parseArrayIp($this->_ip);
        if (stristr($this->_ip, ","))
        {
            $this->objectListConf();
            return false;
        }
        $this->_list = $this->getInBaseConf($this->_ip);
        $this->objectListConf();
        return;
    }

    public function getListConfigAll()
    {
        $this->_obj = new ObjectComposant();
        $this->_list = $this->_obj->listeToutesConfiguration();
        $this->_obj = new confList();
        $this->_obj->conf = $this->_list;
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    public function getConfig()
    {
        $this->_id = $this->_controller->getRequest()->getParam('idconf');
        $this->_obj = new ObjectComposant();
        $a = $this->_obj->rechercheConfigurationId($this->_id);
        $this->cleanConfFolder();
        $c = explode(" ", $a[0]['Date']);
        $b = fopen('./public/tmp/' . $c[0], 'w');
        fwrite($b, $a[0]['Configuration']);
        fclose($b);
        $this->_obj = new fileconf();
        $this->_obj->name = $c[0];
        echo Zend_Json_Encoder::encode($this->_obj);
    }

    private function cleanConfFolder()
    {
        $dir = './public/tmp/';
        $a = opendir($dir);
        while (($b = readdir($a)) !== false)
        {
            if (preg_match('/^[0-9]{2}\-[0-9]{2}\-[0-9]{2}$/', $b) == 1)
                unlink($dir . $b);
        }
        closedir($a);
    }

    private function objectListConf()
    {
        $this->_obj = new confList();
        $this->_obj->model = $this->_param['model'];
        $this->_obj->type = $this->_param['type'];
        $this->_obj->protocol = $this->_param['protocol'];
        $this->_obj->identity = $this->_identity;
        $this->_obj->conf = $this->_list;
        echo Zend_Json_Encoder::encode($this->_obj);
        return;
    }

    private function getInBaseConf()
    {
        $this->_obj = new ObjectComposant();
        $this->_id = $this->_obj->rechercheComposant($this->_ip);
        $this->_list = $this->_obj->rechercheConfiguration($this->_id[0]['IdComposant']);
        return $this->_list;
    }

}
